<template>
  <div class="tag-container">
    <div class="hd">
      <div class="hd-left">
        <el-input
          v-model="searchTitle"
          clearable
          placeholder="请输入标签名"
          style="width: 200px; margin-right: 10px"
          size="small"
        ></el-input>
        <TsButton
          perms="tag:page:get"
          label="搜索"
          type="primary"
          size="small"
          @click="search"
        ></TsButton>
      </div>
      <div class="hd-right">
        <TsButton
          label="添加"
          perms="tag:add:post"
          type="success"
          size="small"
          @click="add"
        ></TsButton>
        <TsButton
          perms="tag:delete"
          label="删除"
          type="danger"
          size="small"
          @click="delMute"
        ></TsButton>
      </div>
    </div>
    <!--    表格区域-->
    <div class="table">
      <div class="table">
        <el-table
          :data="tableData"
          stripe
          style="width: 100%"
          tooltip-effect="dark"
          :header-cell-style="{ background: '#FEF6F6' }"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column prop="name" label="标签名" width="300" />
          <el-table-column label="操作" fixed="right" width="240px">
            <template #default="scope">
              <TsButton
                perms="tag:update:put"
                label="修改"
                style="color: #2968ea"
                type="text"
                size="mini"
                @click="edit(scope.row.id)"
              ></TsButton>
              <TsButton
                perms="tag:delete"
                label="删除"
                style="color: #ea1648"
                type="text"
                size="mini"
                @click="delBgt(scope.row.id)"
              ></TsButton>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!--    分页区域-->
    <el-pagination
      style="float: right; margin: 20px"
      background
      v-model:currentPage="pageNum"
      v-model:page-size="pageSize"
      layout=" prev, pager, next"
      :total="totalNum"
      @size-change="pageSizeChange"
      @current-change="pageCurrentChange"
    />
    <!--    新增编辑对话框-->
    <el-dialog
      v-model="dialogVisible"
      :title="tagId ? '编辑标签' : '新增标签'"
      width="30%"
      :before-close="handleClose"
    >
      <el-input
        placeholder="请输入标签名"
        size="small"
        v-model="tagInput"
      ></el-input>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="submit">确定</el-button>
          <el-button @click="cancel">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import {
  tagPage,
  addTag,
  delTag,
  tagInfo,
  updateTag,
} from "@/api/message/expose";
import commonPageRepository from "@/composables/common-page-repository";
import { ElMessage, ElMessageBox } from "element-plus";

//分页查询数据
const pageNum = ref(1);
const pageSize = ref(50);
const totalNum = ref(0);
const searchTitle = ref("");
//表格数据
const tableData = ref([]);
function getTagList() {
  tagPage({
    pageNumber: pageNum.value,
    pageSize: pageSize.value,
  }).then((res) => {
    tableData.value = res.data.content;
    totalNum.value = res.data.totalElements;
  });
}
//改变分页事件
const { pageSizeChange, pageCurrentChange } = commonPageRepository(
  pageSize,
  pageNum,
  getTagList
);
//搜索事件
function search() {
  pageNum.value = 1;
  tagPage({
    name: searchTitle.value,
    pageNumber: pageNum.value,
    pageSize: pageSize.value,
  }).then((res) => {
    tableData.value = res.data.content;
    totalNum.value = res.data.totalElements;
  });
}
//新增编辑对话框显示与隐藏
const dialogVisible = ref(false);
//关闭对话框之前的回调
function handleClose() {
  dialogVisible.value = false;
  tagInput.value = "";
  tagId.value = "";
}
const tagInput = ref("");
//添加标签
function add() {
  dialogVisible.value = true;
}
//提交方法
function submit() {
  if (!tagId.value) {
    addTag({
      name: tagInput.value,
    }).then((res) => {
      if (res.code === 200) {
        ElMessage.success("添加成功");
        dialogVisible.value = false;
        tagInput.value = "";
        tagId.value = "";
        getTagList();
      }
    });
  } else {
    updateTag({
      id: tagId.value,
      name: tagInput.value,
    }).then((res) => {
      if (res.code === 200) {
        ElMessage.success("更新成功");
        dialogVisible.value = false;
        tagInput.value = "";
        getTagList();
        tagId.value = "";
      }
    });
  }
}
//多选数组
const mutpleArr = ref([]);
//多选事件
function handleSelectionChange(val) {
  mutpleArr.value = val.map((item) => item.id);
}
//多选删除方法
function delMute() {
  if (mutpleArr.value.length === 0) {
    ElMessage.warning("请选择数据后再试");
    return;
  }
  delTag(mutpleArr.value).then((res) => {
    if (res.code === 200) {
      ElMessage.success("删除成功");
      getTagList();
    }
  });
}
const tagId = ref("");
//编辑方法
function edit(id) {
  tagId.value = id;
  dialogVisible.value = true;
  tagInfo(id).then((res) => {
    tagInput.value = res.data.name;
  });
}
//取消方法
function cancel() {
  dialogVisible.value = false;
  tagInput.value = "";
  tagId.value = "";
}
//单个删除标签
function delBgt(id) {
  ElMessageBox.confirm("您确定要删除这条数据么?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delTag([id]).then((res) => {
        if (res.code === 200) {
          ElMessage.success("删除成功");
          getTagList();
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消操作",
      });
    });
}
onMounted(() => {
  getTagList();
});
</script>

<style scoped lang="scss">
.tag-container {
  height: calc(100vh - 280px);
  .hd {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .hd-left {
    }
  }
  .table {
    margin-top: 10px;
    height: calc(100% - 120px);
    overflow-y: auto;
  }
}
</style>
